package com.jcp.service.imple;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jcp.dao.CarOutAndOnMapper;
import com.jcp.entity.Record;
import com.jcp.entity.Users;
import com.jcp.service.carOutAndOn;

@Service
@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED)
public class carOutAndOnImple implements carOutAndOn {
	//依赖注入carOutAndOnMapper对象
	@Autowired
	private CarOutAndOnMapper coam;
	/**
	 * 车辆进入的方法
	 */
	@Override
	public String selectByCarNumber(String carNumber) {

		//通过车牌查询用户表如果有值说明该用户有注册会员或普通用户，没有则是临时停车用户
		List<Users> userList = coam.selectByCarNumber(carNumber);
		if(userList.size()>0) {
			//判断该用户是否有进行包月会员有则对车位表的vip车位减一没有则对普通车位减1
			System.out.println("会员日期 ： "+userList.get(0).getMembership_expires_date());
			if(!userList.get(0).getMembership_expires_date().equals("")) {//会员用户
				//添加车辆进出记录
				coam.insertByRecord(carNumber,2);
				//修改车位表
				coam.updateByCar(carNumber);
				return "欢迎光临vip用户："+userList.get(0).getCarNumber()+"来此停车";
			}else {//普通用户
				coam.insertByRecord(carNumber,1);
				coam.updateByCar(null);
				return "欢迎光临用户："+userList.get(0).getCarNumber()+"来此停车";
			}
		}else {//临时用户
			
			coam.insertByRecord(carNumber,0);
			coam.updateByCar(null);
			return "每小时/5元，为你的爱车保驾护航，祝你停车愉快";
		}
	}
	@Override
	public String selectByCarOut(String carNumber) {

		//通过车牌查询用户表如果有值说明该用户有注册会员或普通用户，没有则是临时停车用户
		List<Users> userList = coam.selectByCarNumber(carNumber);
		if(userList.size()>0) {
			//判断该用户是否有进行包月会员有则对车位表的vip车位减一没有则对普通车位减1
			if(!userList.get(0).getMembership_expires_date().equals("")) {//会员用户
				long time4 = CarOnAndOut(carNumber);
				//修改车位表
				coam.updateByCarOut(carNumber);
				return "vip用户欢迎下次光临:本次以为你节省"+time4+"元";
			}else {//普通用户
				long time4 = CarOnAndOut(carNumber);
				if(userList.get(0).getRemainder()>=Double.parseDouble(Long.toString(time4))) {
					coam.UpdateByMoney((userList.get(0).getRemainder()-Double.parseDouble(Long.toString(time4))), carNumber);
					//修改车位表
					coam.updateByCarOut(null);
					return "欢迎下次光临:本次消费"+time4+"元";
				}else {
					coam.updateByCarOut(null);
					return ""+time4;
				}
			}
		}else {//临时用户
			long time4 = CarOnAndOut(carNumber);
			
			coam.updateByCarOut(null);
			return ""+time4;
		}
	}
	/**
	 * 车辆进出记录的查询
	 * @param carNumber：车牌号
	 * @return
	 */
	private long CarOnAndOut(String carNumber) {
		//获取进出时间
		Record re = coam.selectByTime(carNumber);
		//获取本地时间
		Date date = new Date();
		long time3 = 0;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			long time1 = simpleDateFormat.parse(re.getEnter_time()).getTime();
			System.out.println("time1 : "+time1);
			long time2 = simpleDateFormat.parse(date.toLocaleString()).getTime();
			System.out.println("time2 : "+time2);
			time3 = time1-time2;
			System.out.println("time3 : "+time3);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long time4 = (time3/3600000)*5;
		if(time4==0) {
			time4=5;
		}
		
		//修改车辆进出记录
		coam.updateByRecordCarOut(time4, carNumber);
		return time4;
	}
	@Override
	public Record selectByCarOutandIn(String carNumber) {
		
		return coam.selectByTime(carNumber);
	}
}
